<template>
<!-- 更新任务 -->
<a-card :border='false'>
  <div>
    <my-table-screen
      :queryParam="queryParam"
      :queryOptions="queryOptions"
      :queryData="queryData"
      @handleFilter="handleFilter"
    ></my-table-screen>

    <div>
      <my-table
        ref="mytable"
        :tableData="tableData"
        @tableHandleIn="tableHandleIn"
      ></my-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParam.page"
        :limit.sync="queryParam.page_size"
        @pagination="getList"
      />
    </div>
  </div>

  <!-- <a-drawer
    placement="right"
    :closable="false"
    :destroyOnClose="true"
    :maskClosable="true"
    width="70%"
    :visible="is_drawer"
    @close="drawerClose"
  >
    <tack-details v-if="is_drawer" :tack_details="tack_details"></tack-details>
  </a-drawer> -->

</a-card>
</template>

<script>
import { adUpdateTaskList } from "@/api/adv/batch";

export default {

  data () {
    return {
      is_drawer:false,
      tack_details:'',
      // 查询
      queryParam:{  // 初始化数据
        page:1,
        page_size:10,
      },
      total: 0,
      queryData:[
        { id:'id',title:'ID',type:'input',placeholder:'ID' },
        { id:'task_status',title:'任务状态',type:'select',placeholder:'请选择状态',options:'taskStatus' },
        { id:'task_type',title:'更新类型',type:'select',placeholder:'更新类型',options:'adUpdateType' },
        { id:'created_at',title:'创建时间',type:'times' },
      ],
      queryOptions:{
        oceanTaskStatus:this.$global.taskStatus,
        adUpdateType:this.$global.adUpdateType,
      },
      // 表格
      tableData:{
        rowKey:false,
        rowSelection:false,
        tableDataSource:[],
        tableColumns:[
          { title:'ID',dataIndex:'id',align:'center',width:80 },
          { title:'账户ID',dataIndex:'account_id',align:'center' },
          { title:'计划ID',dataIndex:'ad_id',align:'center' },
          { title:'更新状态',dataIndex:'exec_status',align:'center',scopedSlots: {customRender:'filter'},
            formatter:(row)=>{
              for (const item of this.$global.taskStatus) {
                if( item.value == row.exec_status ) return item.label
              }
            },colorEvent:(row)=>{
              if(row.exec_status == 'WAITING'){ return `color:${ this.$global.colorStatus['orange'] }` }
              else if(row.exec_status == 'SUCCESS'){ return `color:${ this.$global.colorStatus['green'] }` }
              else if(row.exec_status == 'FAIL'){ return `color:${ this.$global.colorStatus['red'] }` }
            }
          },
          { title:'更新类型',dataIndex:'ad_update_type',align:'center',scopedSlots: {customRender:'filter'},
            formatter:(row)=>{
              for (const item of this.$global.adUpdateType) {
                if( item.value == row.ad_update_type ) return item.label
              }
            }
          },
          { title:'操作用户名称',forname:['admin_user','name'],align:'center',scopedSlots: {customRender:'cycles'},
            "formatter":function(obj,name){
              return obj !== null ? obj[name] : '【系统】'
            }
          },
          { title:'开始时间',dataIndex:'start_at',align:'center',width:180 },
          { title:'创建时间',dataIndex:'created_at',align:'center',width:180 },
          { title:'更新时间',dataIndex:'updated_at',align:'center',width:180 },
          {
            title: '操作',align:'center',width:100,
            scopedSlots: {customRender: 'action'},
            tableBtn:[
              { title:'详情',type:'primary',btnType:'handleDetails',size:'small' },
            ]
          },
        ]
      },
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 初始化数据
    getList(){
      adUpdateTaskList(this.queryParam).then((res)=>{
        if(res.code === 'SUCCESS'){
          this.tableData.tableDataSource = res.data.list
          this.total = res.data.page_info.total
        }
      })
    },
    // table表内列按钮
    async tableHandleIn(tableFnc,row){
      // console.info(tableFnc,row,'row')
      let res
      if( tableFnc === 'handleDetails' ) return ( this.tack_details = row,this.is_drawer = true )  // 详情
      // 任务操作
      if ( tableFnc === 'handleOpen' && row.task_status !== 'DONE' ) res = await taskOpen(row.id)
      if ( tableFnc === 'handleClose' && row.task_status !== 'CLOSE' ) res = await taskClose(row.id)
      // 计划操作
      if( tableFnc === 'handleViewAD' ) return this.$router.push( { path:'/promote/plans',query:{ task_id:row.id } } )

      if( tableFnc === 'AdOpen' || tableFnc === 'AdClose' ){
        let quest = {
          ocean_ad_creative_create_task_id:row.id,
          ad_update_type:'STATUS',
          data:{
            opt_status: tableFnc === 'AdOpen' ? 'enable' : 'disable'
          }
        }
        res = await adUpdateTask(quest)
      }
      if( typeof res === 'undefined' ) return this.$Tip('请正确使用按钮')
      this.$Tip(res)
      if( res.code === 'SUCCESS' ) this.getList()
    }, 
    // 搜索
    handleFilter(){
      this.queryParam.page = 1
      this.getList()
    },
    // 关闭抽屉
    drawerClose(){
      this.is_drawer = false
    },
  }
}
</script>
